package com.yuexiang.melodyhub.user.controller;

import com.yuexiang.melodyhub.common.dto.user.UserAdd.param.UserAddParam;
import com.yuexiang.melodyhub.common.dto.user.UserFindByEmail.param.UserFindByEmailParam;
import com.yuexiang.melodyhub.common.dto.user.UserFindByEmail.response.UserFindByEmailResponse;
import com.yuexiang.melodyhub.common.dto.user.UserFindWithRolesByUsername.param.UserFindWithRolesByUsernameParam;
import com.yuexiang.melodyhub.common.dto.user.UserFindWithRolesByUsername.response.UserFindWithRolesByUsernameResponse;
import com.yuexiang.melodyhub.common.result.ResultData;
import com.yuexiang.melodyhub.common.valid.HasRoles;
import com.yuexiang.melodyhub.user.dto.UserChangeUserStatusByUserId.UserChangeUserStatusByUserIdParam;
import com.yuexiang.melodyhub.user.dto.UserDeleteUserByUserId.param.UserDeleteUserByUserIdParam;
import com.yuexiang.melodyhub.user.dto.UserGetBasicInfos.response.UserGetBasicInfosResponse;
import com.yuexiang.melodyhub.user.dto.UserUpdatePassword.param.UserUpdatePasswordParam;
import com.yuexiang.melodyhub.user.dto.UserUpdateUserInfos.param.UserUpdateUserInfosParam;
import com.yuexiang.melodyhub.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public ResultData<?> add(@RequestBody UserAddParam userParam) {
        userService.add(userParam);
        return ResultData.success(null);
    }

    @PostMapping("/findWithRolesByUsername")
    public ResultData<UserFindWithRolesByUsernameResponse> findWithRolesByUsername(@RequestBody UserFindWithRolesByUsernameParam userFindParam) {
        return ResultData.success(userService.findWithRolesByUsername(userFindParam));
    }

    @PostMapping("/findByEmail")
    public ResultData<UserFindByEmailResponse> findByEmail(@RequestBody UserFindByEmailParam userFindByEmailParam) {
        return ResultData.success(userService.findByEmail(userFindByEmailParam));
    }

    @PostMapping("/saveLastTimeByEmail")
    public ResultData<?> saveLastTimeByEmail(@RequestParam("email") String email) {
        userService.saveLastTimeByEmail(email);
        return ResultData.success(null);
    }

    @HasRoles(roles = "admin")
    @PutMapping("/changeUserStatusByUserId")
    public ResultData<?> changeUserStatusByUserId(@RequestBody UserChangeUserStatusByUserIdParam param) {
        userService.changeUserStatusByUserId(param);
        return ResultData.success(null);
    }

    @HasRoles(roles = "admin")
    @DeleteMapping("/deleteUserByUserId")
    public ResultData<?> deleteUserByUserId(@RequestBody UserDeleteUserByUserIdParam param) {
        userService.deleteUserByUserId(param);
        return ResultData.success(null);
    }

    @GetMapping("/getBasicInfos")
    public ResultData<UserGetBasicInfosResponse> getBasicInfos(@RequestParam("email") String email) {
        return ResultData.success(userService.getBasicInfos(email));
    }

    @PutMapping("/updateUserInfos")
    public ResultData<?> updateUserInfos(@RequestBody UserUpdateUserInfosParam param) {
        userService.updateUserInfos(param);
        return ResultData.success(null);
    }

    @PutMapping("/updatePassword")
    public ResultData<?> updatePassword(@RequestBody UserUpdatePasswordParam param) {
        userService.updatePassword(param);
        return ResultData.success(null);
    }

}
